<?php
require_once 'client'.EXT;

class Agents extends Client{
	
	function __construct()
	{
		parent::__construct();
		$this->load->model('user_model');
		$this->data['current']	=	'agent';
	}
	
	function index()
	{
		$p	=	(isset($_GET['p']) ? $_GET['p'] : 1);
		$limit	=	5;
		$offset	=	($p-1)*$limit;
		$where = array();
		if(isset($_GET['username']) && $_GET['username'] != '')
		{
			$where['username']	=	$_GET['username'];
		}
		
		if(isset($_GET['email']) && $_GET['email'] != '')
		{
			$where['email']	=	$_GET['email'];
		}
		$result	=	$this->user_model->get_limit($limit,$offset,$where);
		$this->data['rows']	=	$result['rows'];
		$conf	=	array(
			'base_url'		=>	current_link(),
			'cur_page'		=>	$p,	
			'total_rows'	=>	$result['num_rows'],
			'per_page'		=>	$limit
		);
		$this->pagination->initialize($conf);
		$this->data['pagnav']	=	$this->pagination->display_query_string();
		$this->data['tpl_file']		=	'agents/index';
		$this->load->view('layout/default',$this->data);
	}
	
	function add()
	{
		if($_SERVER['REQUEST_METHOD'] == 'POST')
		{
			$this->form_validation->set_rules('username','UserName','required|callback__check_user');
			$this->form_validation->set_rules('password','Password','required');
			$this->form_validation->set_rules('cpassword','Confirm Password','required|matches[password]');
			$this->form_validation->set_rules('email','Email','required|valid_email');
			$data	=	array(
				'username'	=>	$this->input->post('username'),
				'password'	=>	md5($this->input->post('password')),
				'email'		=>	$this->input->post('email'),
				'status'	=>	$this->input->post('status'),
				'group_id'	=>	$this->input->post('group'),
				'created_on'	=>	date('Y-m-d H:i:s')
			);
			if($this->form_validation->run() == TRUE)
			{
				$this->user_model->insert($data);
				redirect(base_url().'agents/');
			}
			
			
		}
		$this->load->view('agents/add', $this->data);
	}
	
	function edit($id= null)
	{
		$this->data['agent'] = $this->user_model->get_by('user_id',$id);
		
		if($_SERVER['REQUEST_METHOD'] == 'POST')
		{
			if($id){
				if($this->input->post('password') != '')
				{
					$this->form_validation->set_rules('username','UserName','required');
					$this->form_validation->set_rules('password','Password','required');
					$this->form_validation->set_rules('cpassword','Confirm Password','required|matches[password]');
					$this->form_validation->set_rules('email','Email','required|valid_email');
					$data	=	array(
						'username'	=>	$this->input->post('username'),
						'password'	=>	md5($this->input->post('password')),
						'email'		=>	$this->input->post('email'),
						'status'	=>	$this->input->post('status'),
						'group_id'	=>	$this->input->post('group'),
						'created_on'	=>	date('Y-m-d H:i:s')
					);
					if($this->form_validation->run() == TRUE)
					{
						$this->user_model->update($id,$data);
						redirect(base_url().'agents/');
					}
				}//Change password
				else
				{
					$this->form_validation->set_rules('username','UserName','required');
					$this->form_validation->set_rules('email','Email','required|valid_email');
					$data	=	array(
						'username'	=>	$this->input->post('username'),
						'email'		=>	$this->input->post('email'),
						'status'	=>	$this->input->post('status'),
						'group_id'	=>	$this->input->post('group'),
						'created_on'	=>	date('Y-m-d H:i:s')
					);
					if($this->form_validation->run() == TRUE)
					{
						$this->user_model->update($id,$data);
						redirect(base_url().'agents/');
					}
				}//end not change password
			}
		}
		$this->load->view('agents/edit', $this->data);
	}
	
	function del($id = null)
	{
		if($id){
			$this->user_model->delete($id);
			redirect(base_url().'agents/');
		}
	}
	
	function edit_profile($user_id = null)
	{
		$this->load->model(array('user_profile_model','country_model'));
		$profile	=	$this->user_profile_model->get_by(array('user_id' => $user_id));
		$this->data['countries']	=	$this->country_model->get_all();
		if($_SERVER['REQUEST_METHOD'] == 'POST')
		{
			$nationality	=	($this->input->post('other_nationality') ? $this->country_model->insert(array('name' => $this->input->post('other_nationality'))) : $this->input->post('nationatily'));
			$this->form_validation->set_rules('fullname','FullName','required');
			$data	=	array(
					'title'				=>	$this->input->post('title'),
					'fullname'			=>	$this->input->post('fullname'),
					'nationality'		=>	$nationality,
					'passport_no'		=>	$this->input->post('passport'),
					'marital_status'	=>	$this->input->post('marital'),
					'date_of_birth'		=>	convert_date_mysql($this->input->post('birth')),
					'smoker'			=>	$this->input->post('smoker'),
					'occupation'		=>	$this->input->post('occupation'),
					'user_id'			=>	$user_id
			);
			if($this->form_validation->run() == TRUE){
				if($profile)
				{
					$this->user_profile_model->update($profile->id,$data);
				}//update
				else
				{
					$this->user_profile_model->insert($data);
				}//insert profile
				redirect(base_url().'agents/');
			}
		}
		$this->data['profile']	=	$profile;
		$this->load->view('agents/edit_profile', $this->data);
	}
	
	function _check_user($username = null)
	{
		if($this->user_model->get_by(array('username' => $username)))
		{
			$this->form_validation->set_message('_check_user','The %s ...');
			return FALSE;
		}
		return TRUE;
	}
}